﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using Microsoft.SharePoint;
using iTechnology.Tools.Entities;
using iTechnology.Tools.Sharepoint;
using System.IO;
using System.Reflection;
using iTechnology.Tools;

namespace iTechnology.Tools
{

    public partial class MainForm : Form
    {


        #region Properties
        SharePointHelper helper = new SharePointHelper();
        SPWeb currentWeb = null;
        TreeNode currentNode = null;
        #endregion

        
        public MainForm()
        {
            if (!helper.IsSharepointExist)
                return;
            InitializeComponent();
        }

        private void MainForm_Load(object sender, EventArgs e)
        {
            
            tvSharepointTree.ImageList = imageList;
            //Build the tree that represents the SharePoint local farm. 
            tvSharepointTree.Nodes.Add(helper.BuildTree());
        }

        private void tvSharepointTree_AfterSelect(object sender, TreeViewEventArgs e)
        {
            currentNode = e.Node;
            if (e.Node.Tag is SPWeb)
            {

                //Populate the grid with the lists that were discovered to be orhpan and also
                //the available list templates that will be used to populate the dropdown.
                List<OrphanList> orphanLists = null;
                this.currentWeb = e.Node.Tag as SPWeb;
                helper.GetWebNode(this.currentWeb, currentNode, out orphanLists);
                helper.ClearCachedItems();

                damagedListsBindingSource.DataSource = orphanLists;
                listTemplateBindingSource.DataSource = helper.FindListTemplates(this.currentWeb);
                btnFix.Enabled = true;

            }
            else
            {
                listTemplateBindingSource.Clear();
                damagedListsBindingSource.Clear();
                btnFix.Enabled = false;
            }

        }

        private void btnFix_Click(object sender, EventArgs e)
        {
            SharepointListsDataContext context = new SharepointListsDataContext(this.currentWeb.Site.ContentDatabase.DatabaseConnectionString);
            List<OrphanList> orphanLists = (List<OrphanList>)damagedListsBindingSource.DataSource;

            //Get all the selected list instances that also have selected list templates and update the tp_ID column in the AllList table in the 
            //site content database. 
            foreach (OrphanList orphanList in orphanLists)
            {
                if (orphanList.Selected && orphanList.SelectedTemplateFeatureId != Guid.Empty)
                {
                    AllList item = (from l in context.AllLists where l.tp_ID == orphanList.ListId select l).First<AllList>();
                    item.tp_FeatureId = orphanList.SelectedTemplateFeatureId;
                }
            }
            orphanLists = null;
            //Run with elevated privileges since a direct database operation will be executed. 
            SPSecurity.RunWithElevatedPrivileges(delegate()
             {
                 context.SubmitChanges();
                 using (SPSite site = new SPSite(this.currentWeb.Url))
                 {
                     this.currentWeb.Close();
                     //Recreate the updated site tree node in order to reflect the changes. 
                     TreeNode newNode = helper.GetWebNode(site.OpenWeb(), this.currentNode, out orphanLists);
                     
                     this.currentWeb = currentNode.Tag as SPWeb;
                 }
             });
            damagedListsBindingSource.DataSource = orphanLists;
            listTemplateBindingSource.DataSource = helper.FindListTemplates(this.currentWeb);
        }

        private void lnk_LinkClicked(object sender, LinkLabelLinkClickedEventArgs e)
        {
            System.Diagnostics.Process.Start("IExplore", ((LinkLabel)sender).Tag as string);
        }
    }
}
